#######################################################################################
### R file to replicate the figures for the three illustrations in Laron K Williams, "Compression, Temporal Dependence, and the Sensitivity of Quantities of Interest"
###
### Created: 11-21-14
### Modified: 3-6-18
###
#######################################################################################

library(foreign)
library(ggplot2)
library(lattice)
library(fields)
library(separationplot)
library(scales)
library(gridExtra)

# Set working directory
#setwd("")

# Font size
f1.ftsize <- 18
f2.ftsize <- 15


#######################################################################################
### Figure 1: Partial effects of personalist regimes on the probability of nuclear weapons pursuit across values of t: Way and Weeks (2014)
#######################################################################################
ww2 = read.dta("Way and Weeks 2014/Data/WW2.dta", convert.underscore=TRUE)

t <- ggplot(ww2, aes(time, pe)) + geom_point() + xlab("Years Since Last Pursuit") + ylab(expression(paste(Delta, "Pr(y = 1)")))
t <- t + geom_line(data = ww2, aes(x = time, y = ape.t), colour = "blue", size = 1.25) + theme_minimal() + theme(axis.title = element_text(size = f1.ftsize))
t

#######################################################################################
### Figure 2: Average partial effects of an increase in logged factions...across values of civil war incidence: Cunningham (2013)
#######################################################################################
cu = read.dta("Cunningham 2013/Data/Cunningham.dta", convert.underscore = TRUE)
ch = read.dta("Cunningham 2013/Data/chist.dta", convert.underscore = TRUE)
ch <- subset(ch, time <= 40)

### Scatterplot with circles denoting in-sample mean, median and mode
cd <- ggplot() + geom_line(data = cu, aes(x = time, y = ape.t), colour = "blue", size = 1.25) + geom_point(data = cu, aes(x = time, y = pe)) + xlab("") + ylab(expression(paste(Delta, "Pr(y = 1)"))) + theme_minimal()
cd <- cd + geom_point(aes(x = 15, y = 0.226), shape = 1, size = 15, colour = "red") + geom_point(aes(x = 10, y = 0.097), shape = 1, size = 15, colour = "red")+ geom_point(aes(x = 0, y = 0.324), shape = 1, size = 15, colour = "red")

### Average first differences: mean, median and mode: with histogram
hist_b <- ggplot() + geom_histogram(data=ch, aes(x=time), fill = "grey50", color = "grey90")+ xlab("Years Since Civil War Onset") + ylab("") + theme_minimal() + theme(axis.title = element_text(size = f1.ftsize))

### Put the two panels on the same figure
grid.newpage()
pushViewport(viewport(layout = grid.layout(4,1)))

vplayout <- function(x, y) viewport(layout.pos.row = x, layout.pos.col = y)
print(cd, vp = vplayout(1:3, 1))
print(hist_b, vp = vplayout(4, 1))


#######################################################################################
### Figure 5: Partial effects of a war tax of Party across Temporal dependence values: Flores-Macias and Kreps (2013)
#######################################################################################
fmko = read.dta("Flores-Macias and Kreps 2013/Data/Observed-Case Partial Effects--FMK.dta", convert.underscore = TRUE)
fmkr = read.dta("Flores-Macias and Kreps 2013/Data/FMKdist.dta", convert.underscore = TRUE)

### Scatterplot of first differences
fdfmk <- ggplot() + geom_line(data = fmko, aes(x = yearsnotax, y = pe.mn), colour = "blue", size = 1.25) + geom_point(data = fmko, aes(x = yearsnotax, y = pe)) + geom_hline(yintercept = 0.061, linetype = "dashed") +  xlab("") + ylab(expression(paste(Delta, "Pr(y = 1)"))) + theme_minimal()

### Histogram
hist <- ggplot() + geom_histogram(data=fmkr, aes(x=yearsnotax), fill = "grey50", color = "grey90")+ xlab("Years Since War Tax") + ylab("") + theme_minimal() + theme(axis.title = element_text(size = f1.ftsize))

### Put the two panels on the same figure
grid.newpage()
pushViewport(viewport(layout = grid.layout(4,1)))

vplayout <- function(x, y) viewport(layout.pos.row = x, layout.pos.col = y)
print(fdfmk, vp = vplayout(1:3, 1))
print(hist, vp = vplayout(4, 1))


